'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 英雄
 * @author sundbcn@gmail.com (sun debing)
 */

var _ = require('lodash');
var heroConf = require('../../config/heroConf');
var constantUtil = require('../../util/constantUtil');
var attrFromAtkType = require('./attrFromAtkType');
var attrType = require('../../consts/attrType');
var dataApi = require('../dataApi');
var soldierType = require('../../consts/soldierType');
var skillAttr = require('./skillAttr');
var equipPropertyService = require('../../services/equipPropertyService');
var buildType = require('../../consts/buildType');
var buildConf = require('../../config/buildConf');

exports.getAttr = function (role_model, bag_model, skill_model, build_model) {
    // 获取属性的列表: 包括装备,附加属性
    var property_list = equipPropertyService.getPropertyList(bag_model);

    // 获取英雄的属性
    var hero_attr = _getAttr(role_model, build_model, property_list);
    hero_attr.equip_list = _getEquipAttr(bag_model);
    hero_attr.skill_list = _getSkillAttr(skill_model, property_list);
    return hero_attr;
};

/**
 * 获取装备的属性
 * @param  {[type]} bag_model [description]
 * @return {[type]}           [description]
 */
var _getEquipAttr = function (bag_model) {
    var wear_equip_list = bag_model.getWearEquipList();
    var equip_list = [];
    _.each(wear_equip_list, function (equip_item) {
        equip_list.push(equip_item.getType());
    });
    return equip_list;
};

var _getSkillAttr = function (skill_model, property_list) {
    var skill_data = [];
    var select_skill_list = skill_model.getSelectSkillList();

    var sub_killl_time = property_list[attrType.REDUCE_CD].value1;
    _.each(select_skill_list, function (skill_type) {
        var skill_item = skill_model.getSkill(skill_type);
        var skill_attr = skillAttr.getAttr(skill_type, skill_item.getLv());
        skill_attr.cd_time *= (1 - sub_killl_time / 100);
        skill_data.push(skill_attr);
    });
    return skill_data;
};

/**
 * 获取英雄属性
 */
var _getAttr = function (role_model, build_model, property_list) {
    var hero_type_conf = heroConf.getHeroTypeConf(role_model.getJob());
    var hero_lv_conf = heroConf.getHeroLvConf(role_model.getLv());
    var hero_attr_lv_conf = heroConf.getHeroAttrLvConf(role_model.getJob(), role_model.getLv());
    var atk_type_attr = attrFromAtkType.getAttr(hero_type_conf.atk_type, hero_attr_lv_conf.atk);

    // 英雄的原始属性
    var hero_attr = {
        // 最小攻击力
        min_atk: atk_type_attr.min_atk,

        // // 最大攻击力
        // max_atk: atk_type_attr.max_atk,

        // // 火属性伤害
        // fire_damage: atk_type_attr.fire_damage,

        // // 冰属性伤害
        // ice_damage: atk_type_attr.ice_damage,

        // // 电属性伤害
        // light_damage: atk_type_attr.light_damage,

        // // 攻城属性伤害
        // city_damage: atk_type_attr.city_damage,

        // // 抵抗物理伤害
        // physical_def: 0,

        // // 抵抗火焰伤害
        // fire_def: 0,

        // // 抵抗冰霜伤害
        // ice_def: 0,

        // // 抵抗雷电伤害
        // light_def: 0,

        // // 抵抗攻城伤害
        // city_def: 0,

        // // 物理伤害反弹
        // physical_bounce: 0,

        // // 火焰伤害反弹
        // fire_bounce: 0,

        // // 冰霜伤害反弹
        // ice_bounce: 0,

        // // 雷电伤害反弹
        // light_bounce: 0,

        // // 攻城伤害反弹
        // city_bounce: 0,

        // 血量
        hp: hero_attr_lv_conf.hp,

        // 命中率
        hit: 100,

        // 闪避率
        dodge: 0,

        // 爆击率
        crit: hero_attr_lv_conf.crit || 0,

        // 反爆击率
        uncrit: hero_attr_lv_conf.uncrit || 0,

        // 爆击增加值
        crit_addition: constantUtil.value('crit_addition'),

        // 攻击间隔
        atk_interval: hero_type_conf.atk_interval,

        // 攻击范围
        atk_range: hero_type_conf.atk_range,

        // 警界范围
        warn_range: hero_type_conf.warn_range,

        // 速度
        speed: hero_type_conf.speed,

        // // 弱点
        // weakness_list: [],

        // // 抗性
        // resistance_list: []
    };

    hero_attr.uncrit += property_list[attrType.UN_CRIT].value1;
    hero_attr.hp *= (1 + property_list[attrType.HP_ADDITION].value1 / 100);
    var original_attr = _.clone(hero_attr, true);
    // hero_attr = _getHeroAttrWithTalentAdd(hero_attr, original_attr, role_model.choice_talent);
    hero_attr = _getHeroAttrWithConstellation(hero_attr, original_attr, role_model.constellation_lv);
    hero_attr = _getHeroAttrWithPropertyList(hero_attr, property_list);
    return {
        // 英雄职业
        hero_type: role_model.getJob(),

        // 攻击速度
        atk_speed: property_list[attrType.ATK_SPEED].value1,

        // 击杀回复血量
        kill_recover_hp: property_list[attrType.KILL_RECOVER_HP].value1,

        // 击中回复血量
        hit_recover_hp: property_list[attrType.HIT_RECOVER_HP].value1,

        // 每秒回复血量
        recover_hp_every_sec: property_list[attrType.RECOVER_HP_EVERY_SECOND].value1,

        // 属性
        attr: hero_attr
    };
};

/* jshint maxstatements:50 */
/**
 * 读取英雄的属性
 * @param  {[type]} hero_attr     [description]
 * @param  {[type]} property_conf [description]
 * @return {[type]}               [description]
 */
var _getHeroAttrWithPropertyList = function (hero_attr, property_list) {
    hero_attr.min_atk += property_list[attrType.PHYSICAL_DAMAGE].value1 * (1 + property_list[attrType.ADDITION_PHYSICAL_DAMAGE].value1 / 100);
    // hero_attr.max_atk += property_list[attrType.PHYSICAL_DAMAGE].value2 * (1 + property_list[attrType.ADDITION_PHYSICAL_DAMAGE].value1 / 100);
    // hero_attr.fire_damage += property_list[attrType.FIRE_DAMAGE].value1 * (1 + property_list[attrType.ADDITION_FIRE_DAMAGE].value1 / 100);
    // hero_attr.ice_damage += property_list[attrType.ICE_DAMAGE].value1 * (1 + property_list[attrType.ADDITION_ICE_DAMAGE].value1 / 100);
    // hero_attr.light_damage += property_list[attrType.LIGHT_DAMAGE].value1 * (1 + property_list[attrType.ADDITION_LIGHT_DAMAGE].value1 / 100);

    // hero_attr.physical_def += property_list[attrType.DEF_PHYSICAL_DAMAGE].value1;
    // hero_attr.fire_def += property_list[attrType.DEF_FIRE_DAMAGE].value1;
    // hero_attr.ice_def += property_list[attrType.DEF_ICE_DAMAGE].value1;
    // hero_attr.light_def += property_list[attrType.DEF_LIGHT_DAMAGE].value1;
    // hero_attr.city_def += property_list[attrType.DEF_CITY_DAMAGE].value1;

    // hero_attr.physical_bounce += property_list[attrType.BOUNCE_PHYSICAL_DAMAGE].value1;
    // hero_attr.fire_bounce += property_list[attrType.BOUNCE_FIRE_DAMAGE].value1;
    // hero_attr.ice_bounce += property_list[attrType.BOUNCE_ICE_DAMAGE].value1;
    // hero_attr.light_bounce += property_list[attrType.BOUNCE_LIGHT_DAMAGE].value1;
    // hero_attr.city_bounce += property_list[attrType.BOUNCE_CITY_DAMAGE].value1;

    hero_attr.hp += property_list[attrType.HP].value1;
    hero_attr.crit += property_list[attrType.CRIT].value1;
    hero_attr.crit_addition += property_list[attrType.CRIT_ADDITION].value1;
    hero_attr.speed *= (1 + property_list[attrType.SPEED].value1 / 100);
    // hero_attr.city_damage += property_list[attrType.BUILD_DAMAGE].value1;

    hero_attr.ratio_break_effect = property_list[attrType.RATION_BREAK_EFFECT].value1;
    hero_attr.ratio_burn_effect = property_list[attrType.RATION_BURN_EFFECT].value1;
    hero_attr.ratio_ice_effect = property_list[attrType.RATION_ICE_EFFECT].value1;
    hero_attr.ratio_palsy_effect = property_list[attrType.RATION_PALSY_EFFECT].value1;

    hero_attr.ratio_un_break_effect = property_list[attrType.DEF_BREAK_EFFECT].value1;
    hero_attr.ratio_un_burn_effect = property_list[attrType.DEF_BURN_EFFECT].value1;
    hero_attr.ratio_un_ice_effect = property_list[attrType.DEF_ICE_EFFECT].value1;
    hero_attr.ratio_un_palsy_effect = property_list[attrType.DEF_PALSY_EFFECT].value1;

    // 取整
    hero_attr.min_atk = Math.floor(hero_attr.min_atk);
    // hero_attr.max_atk = Math.floor(hero_attr.max_atk);
    // hero_attr.fire_damage = Math.floor(hero_attr.fire_damage);
    // hero_attr.ice_damage = Math.floor(hero_attr.ice_damage);
    // hero_attr.light_damage = Math.floor(hero_attr.light_damage);
    // hero_attr.hp = Math.floor(hero_attr.hp);

    return hero_attr;
};

/**
 * 英雄天赋加成
 * @param  {[type]} hero_attr     [description]
 * @param  {[type]} property_conf [description]
 * @return {[type]}               [description]
 */
var _getHeroAttrWithTalentAdd = function (hero_attr, original_attr, curr_talent) {
    var hero_tallent_conf = dataApi.hero_talent.findById(curr_talent);
    if (!hero_tallent_conf) {
        console.warn('英雄天赋无效: ', curr_talent);
        return;
    }
    var talent_add = hero_tallent_conf.value;
    if (curr_talent === 1) {
        // 加生命
        hero_attr.hp += Math.ceil(original_attr.hp * talent_add / 100);
    } else if (curr_talent === 2) {
        //加攻击
        hero_attr.min_atk += Math.ceil(original_attr.min_atk * talent_add / 100);
        // hero_attr.max_atk += Math.ceil(original_attr.max_atk * talent_add / 100);
        // hero_attr.fire_damage += Math.ceil(original_attr.fire_damage * talent_add / 100);
        // hero_attr.ice_damage += Math.ceil(original_attr.ice_damage * talent_add / 100);
        // hero_attr.light_damage += Math.ceil(original_attr.light_damage * talent_add / 100);
    }

    return hero_attr;
};

/**
 * 英雄星座加成
 * @param  {[type]} hero_attr     [description]
 * @param  {[type]} property_conf [description]
 * @return {[type]}               [description]
 */
var _getHeroAttrWithConstellation = function (hero_attr, original_attr, constellation_lv) {
    if (!constellation_lv) {
        return hero_attr;
    }
    var constllation_conf = dataApi.hero_constellation_sum.findById(constellation_lv);
    if (!constllation_conf) {
        console.warn('英雄星座无效: ', constellation_lv);
        return hero_attr;
    }
    hero_attr.min_atk += constllation_conf.atk;
    hero_attr.hp += constllation_conf.hp;
    hero_attr.crit += constllation_conf.crit;
    hero_attr.dodge += constllation_conf.dodge;
    hero_attr.crit_addition += constllation_conf.crit_atk;
    if (constllation_conf.damage_reduction > 0) {
        hero_attr.damage_sub = hero_attr.damage_sub || 0;
        hero_attr.damage_sub += constllation_conf.damage_reduction;
    }

    return hero_attr;
};
